package com.beyond.trace.controller;

import java.io.IOException;
import java.util.List;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.beyond.trace.domain.TraceProduct;
import com.beyond.trace.service.ITraceProductService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

/**
 * 产品信息Controller
 * 
 * @author yao
 * @date 2022-03-02
 */
@RestController
@RequestMapping("/trace/product")
@Api(value = "产品信息", tags = "产品信息")
public class TraceProductController extends BaseController
{
    @Autowired
    private ITraceProductService traceProductService;

    //@PreAuthorize("@ss.hasPermi('beyond:product:list')")
    @GetMapping("/list")
    @ApiOperation("查询产品信息列表")
    public TableDataInfo list(TraceProduct traceProduct)
    {
        startPage();
        List<TraceProduct> list = traceProductService.selectTraceProductList(traceProduct);
        return getDataTable(list);
    }

    //@PreAuthorize("@ss.hasPermi('beyond:product:query')")
    @GetMapping(value = "/query")
    @ApiOperation("查询产品信息列表不分页")
    public AjaxResult getInfoQuery(TraceProduct traceProduct)
    {
        return AjaxResult.success(traceProductService.selectTraceProductList(traceProduct));
    }

    //@PreAuthorize("@ss.hasPermi('beyond:product:export')")
    @Log(title = "产品信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TraceProduct traceProduct)
    {
        List<TraceProduct> list = traceProductService.selectTraceProductList(traceProduct);
        ExcelUtil<TraceProduct> util = new ExcelUtil<TraceProduct>(TraceProduct.class);
        return util.exportExcel(list, "产品信息数据");
    }

    //@PreAuthorize("@ss.hasPermi('beyond:product:query')")
    @GetMapping(value = "/{productId}")
    @ApiOperation("查询产品信息列表")
    public AjaxResult getInfo(@PathVariable("productId") Long productId, HttpServletRequest request)
    {
        return AjaxResult.success(traceProductService.selectTraceProductById(productId,request));
    }

    @GetMapping(value = "/base")
    @ApiOperation("查询产品对应的基地信息")
    public AjaxResult getProductBase(@RequestParam("productId") Long productId)
    {
        return AjaxResult.success(traceProductService.selectTraceProductBase(productId));
    }

    //@PreAuthorize("@ss.hasPermi('beyond:product:add')")
    @Log(title = "产品信息", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增产品信息")
    public AjaxResult add(@RequestBody TraceProduct traceProduct)
    {
        return toAjax(traceProductService.insertTraceProduct(traceProduct));
    }

    //@PreAuthorize("@ss.hasPermi('beyond:product:edit')")
    @Log(title = "产品信息", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改产品信息")
    public AjaxResult edit(@RequestBody TraceProduct traceProduct)
    {
        return toAjax(traceProductService.updateTraceProduct(traceProduct));
    }

    //@PreAuthorize("@ss.hasPermi('beyond:product:remove')")
    @Log(title = "产品信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{productIds}")
    @ApiOperation("删除产品信息")
    public AjaxResult remove(@PathVariable Long[] productIds)
    {
        if(StringUtils.isEmpty(productIds)) {
            return AjaxResult.error("id不能为空");
        }
        try {
            return toAjax(traceProductService.deleteTraceProductByIds(productIds));
        }catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }

    }

    @Log(title = "产品图片上传", businessType = BusinessType.INSERT)
    @PostMapping("/picture/upload")
    @ApiOperation("图片上传接口")
    public AjaxResult avatar(@RequestParam("file") MultipartFile file) throws IOException {
        try {
            if (!file.isEmpty()) {
                String avatar = FileUploadUtils.upload(RuoYiConfig.getProfile()+"/product", file);
                AjaxResult ajax = AjaxResult.success();
                ajax.put("path", avatar);
                return ajax;
            } else {
                return AjaxResult.error("上传图片异常，请联系管理员");
            }
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }
}
